﻿using System;
using System.Web.UI.WebControls;
using Resources;
using Yglu.BusinessLayer;
using Yglu.BusinessLayer.Entities;
using Yglu.BusinessLayer.Enums;

namespace Yglu.WebAdmin
{
	public partial class Categories_aspx : BasePage
	{
		#region Main methods

		protected void Page_Load(object sender, EventArgs e)
		{
			CheckPermissions(AdminLevel.Company);

			if (!IsPostBack)
			{
				PanEdit.Visible = false;

				SetDefaults();
			}
		}

		protected void Page_PreRender(Object sender, EventArgs e)
		{
			if (PanGrid.Visible)
			{
				LoadData();
			}
		}

		private void SetDefaults()
		{
			SetDefaults(GridCategories);

			TxtCategoryId.Enabled = false;

			if (SessionManager.LoggedUser.AdminLevel >= AdminLevel.Company)
			{
				CboStore.DataSource = Settings.Stores;
				CboStore.DataBind();
			}
			else
			{
				ButAddNew.Visible = false;
				CboStore.Enabled = false;

				CboStore.Items.Add(new ListItem(SessionManager.LoggedStore.Title, SessionManager.LoggedStore.Id.ToString()));
			}

			CboStore.Items.Insert(0, new ListItem(Messages.AllStores, "0"));
			CboStore.SelectedValue = SessionManager.LoggedStore.Id.ToString();
		}

		private void LoadData()
		{
			if (SessionManager.LoggedStore.Id > 0)
			{
				GridCategories.DataSource = new CategoryCollection(DataAccess.Category_Get(SessionManager.LoggedStore));
			}
			else
			{
				GridCategories.DataSource = new CategoryCollection(DataAccess.Category_Get());
			}

			GridCategories.DataBind();

			LblGridCount.Text = GetLocalResourceObject("LblGridCount.Text").ToString().Replace("#", GridCategories.Rows.Count.ToString());

			PanEdit.Visible = false;
			PanGrid.Visible = true;
		}

		#endregion

		#region Add and edit

		protected void ButAddNew_Click(Object sender, EventArgs e)
		{
			TxtCategoryId.Text = "0";
			TxtCategoryTitle.Text = String.Empty;
			TxtCategoryPriority.Text = String.Empty;
            ChkCategoryMultiSelect.Checked = true;
			ChkCategoryActive.Checked = true;

			LitEditLegend.Text = GetLocalResourceObject("LitAddCategory.Text").ToString();
			PanEdit.Visible = true;
			PanGrid.Visible = false;
		}

		protected void GridCategories_RowCommand(Object sender, GridViewCommandEventArgs e)
		{
			EditCategory(Convert.ToInt32(e.CommandArgument));
		}

		private void EditCategory(Int32 row)
		{
			Int32 id = Convert.ToInt32(GridCategories.Rows[row].Cells[0].Text);
			Category p = new Category(id);

			TxtCategoryId.Text = p.Id.ToString();
			TxtCategoryTitle.Text = p.Title;
			TxtCategoryPriority.Text = p.Priority.ToString();
			ChkCategoryMultiSelect.Checked = p.MultiSelect;
			ChkCategoryActive.Checked = p.Active;
			CboStore.SelectedValue = p.StoreId.ToString();

            LitEditLegend.Text = GetLocalResourceObject("LitEditCategory.Text").ToString();
            PanEdit.Visible = true;
			PanGrid.Visible = false;
		}

		#endregion

		#region Save to database

		protected void ButSave_Click(Object sender, EventArgs e)
		{
			Category p = new Category(Convert.ToInt32(TxtCategoryId.Text));

			try
			{
				p.Title = TxtCategoryTitle.Text.Trim();
				p.Priority = Convert.ToInt32(TxtCategoryPriority.Text.Trim());
				p.MultiSelect = ChkCategoryMultiSelect.Checked;
				p.Active = ChkCategoryActive.Checked;

				if (CboStore.SelectedValue == "0")
				{
					p.AllStores = true;
				}
				else
				{
					p.StoreId = Convert.ToInt32(CboStore.SelectedValue);
				}

				try
				{
					DataAccess.Category_Upsert(p);

					TxtCategoryId.Text = p.Id.ToString();

					ClientScript.RegisterStartupScript(typeof(String), "CategorySaved", "YgluTopAlert('" + Messages.ConfirmationDataSaved + "', true);", true);

					PanGrid.Visible = true;
				}
				catch (Exception ex)
				{
					Master.SetError(ex, Messages.ErrorCouldNotSaveData);
				}
			}
			catch (Exception ex)
			{
				Master.SetError(ex, Messages.ErrorInvalidForm);
			}
		}

		protected void ButCancel_Click(Object sender, EventArgs e)
		{
			PanEdit.Visible = false;
			PanGrid.Visible = true;
		}

		#endregion
	}
}